const sRoot = __dirname;
const BaseFinder = require('./base-finder');
const SlocChecker = require('./sloc-checker');
const JscpdChecker = require('./jscpd-checker');
const EslintChecker = require('./eslint-checker');
const {mkdirSync} = require('./utils/fs.js');

class CodeQualityChecker extends BaseFinder {
  constructor(...oPropsArgs) {
    super(...oPropsArgs);

    this.oRes;
  }

  async init(...oArgs) {
    super.fnCheck(...oArgs);
    // 下传参数
    this.oRes = {
      oCheckOptions: this.oCheckOptions, // check重设的参数，优先级高
      aPathPatterns: this.aPathPatterns, // 扫描白名单规则
      sRootPath: this.sRootPath, // 根目录
      aFileList: this.aFileList, // 扫描出来的文档
      sReportPath: this.sReportPath, // 输出路径
    }

    // 创建输出目录
    mkdirSync(this.sReportPath);

    // sloc
    await this.fnSlock();

    // eslint
    await this.fnEslint();

    // jscpd
    await this.fnJscpd();

    console.log("完成");
  }

  fnSlock(){
    console.log("代码占比 -> start");

    const newSlocChecker = new SlocChecker();
    newSlocChecker.fnCheck(this.oRes);
    const oRe = newSlocChecker.fnReport({
      sReportPath:this.sReportPath,
      sFormat: this.oCheckOptions.sFormat
    });

    console.log(`代码占比 -> ${oRe}`);
    console.log("================");
  }

  async fnEslint(){
    console.log("Eslint -> start");
    const newEslintChecker = new EslintChecker();
    await newEslintChecker.fnCheck(this.oRes);
    const oRe = newEslintChecker.fnReport({
      sReportPath:this.sReportPath,
      sFormat: this.oCheckOptions.sFormat,
      sRootPath: this.oRes['sRootPath']
    });

    console.log(`Eslint -> ${oRe}`);
    console.log("================");
  }

  async fnJscpd() {
    console.log("重复率 -> start");

    const newJscpdChecker = new JscpdChecker();
    const oRe = await newJscpdChecker.fnCheck(this.oRes);

    console.log(`重复率 -> ${oRe}`);
    console.log("================");
  }

}

module.exports = CodeQualityChecker;
